<template>
    <div class="">
        <div class="">
            <el-dialog :title="isAdd ? '添加商品分类' : '编辑商品分类'" :visible.sync="isShow" center :before-close="cancel">
                <el-form ref="cateForm" :model="cateForm" :rules="rules">
                    <el-form-item label="上级分类" :label-width="formLabelWidth" prop="pid">
                        <el-select placeholder="请选择" v-model="cateForm.pid">
                            <el-option label="顶级分类" :value="0"></el-option>
                            <el-option v-for="item in catelist" :key="item.id" :value="item.id"
                                :label="item.catename"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="商品分类名称" :label-width="formLabelWidth" prop="catename">
                        <el-input autocomplete="off" v-model="cateForm.catename"></el-input>
                    </el-form-item>
                    <el-form-item label="图片" :label-width="formLabelWidth">
                        <el-upload action="#" list-type="picture-card" :auto-upload="false" :on-preview="onPreview"
                            :on-remove="onRemove" :on-change="onChange" :file-list="fileList" :on-exceed="handleExceed"
                            :limit="1">
                            <i class="el-icon-plus"></i>
                        </el-upload>
                        <el-dialog :visible.sync="dialogVisible">
                            <img width="100%" :src="dialogImageUrl" alt="" />
                        </el-dialog>
                    </el-form-item>
                    <el-form-item label="状态" :label-width="formLabelWidth">
                        <el-switch active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
                            v-model="cateForm.status" :inactive-value="2">
                        </el-switch>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="cancel">取 消</el-button>
                    <el-button type="primary" @click="confirm">确 定</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>
<script>
import { cateAdd, cateEdit, getcateInfo } from '../../request/api'
import { mapActions, mapGetters } from 'vuex'
export default {
    props: ['isShow', 'isAdd'],
    data() {
        return {
            formLabelWidth: '120px',
            cateForm: {
                pid: 0,
                catename: '',
                img: '',
                status: 1
            },
            rules: {
                catename: [
                    { required: true, message: "请输入商品分类名称", trigger: "blur" },
                    { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
                ],
                pid: [{ required: true, message: "请选择上级分类", trigger: "change" }],
            },
            dialogImageUrl: '',
            dialogVisible: false,
            fileList: []
            // value:''
        }
    },
    computed: {
        ...mapGetters({
            catelist: 'classify/getCate'
        })
    },
    mounted() {
        // this.getCateAction()
    },
    methods: {
        // 超出文件上传的回调事件
        handleExceed(files, fileList) {
            this.$message.warning(`当前限制选择1个文件,本次选择了${files.length}个文件,共选择了${files.length + fileList.length}个文件`)
        },
        // 图片改变时发生的变化
        onChange(files, fileList) {
            console.log(files, '文件');
            console.log(fileList, '文件列表');
            this.cateForm.img = files.raw
        },
        // 图片预览
        onPreview(file) {
            // 显示弹框
            this.dialogVisible = true
            this.dialogImageUrl = file.url
        },
        // 图片的移除
        onRemove(file, fileList) {
            console.log(file, '移除文件')
            console.log(fileList, '移除文件列表')
            this.cateForm.img = ''
        },
        cancel() {
            this.$emit('cancel', false)
            this.reset()
        },
        ...mapActions({
            getCateAction: 'classify/changeCateAction'
        }),
        //封装一个重置方法
        reset() {
            this.cateForm = {
                pid: 0,
                catename: '',
                img: '',
                status: 1
            },
                //resetFields	对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
                this.$refs.cateForm.resetFields();
            this.fileList = []
        },
        // 封装一个获取一条数据事件
        lookup(id) {
            // console.log(id);
            getcateInfo({ id }).then(res => {
                console.log(res, 'lookup');
                this.cateForm = res.list
                this.cateForm.id = id
                // 图片
                this.fileList  =this.cateForm.img ? [{ url: "http://localhost:3000" + this.cateForm.img }] : []
            })
        },
        confirm() {
            this.$refs.cateForm.validate((val) => {
                if (val) {
                    if (this.isAdd) {
                        cateAdd(this.cateForm).then((res) => {
                            // console.log(res, 'add');
                            if (res.code == 200) {
                                this.$message.success(res.msg);
                                this.cancel()
                                this.getCateAction()
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                    } else {
                        cateEdit(this.cateForm).then((res) => {
                            console.log(res);
                            if (res.code == 200) {
                                this.$message.success(res.msg);
                                // 关闭弹框
                                this.cancel()
                                // 调用列表接口
                                this.getCateAction()
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                    }
                }
            })
        }
    }
}
</script>
<style scoped>

</style>